using UnityEngine;

public class ControllerPushBox : ControllerGeneric
{
	public float length = 1f;

	public float width = 1f;

	public float height = 1f;

	public GameObject box;

	public float density = 3f;

	public PushObjectType objectType = PushObjectType.box;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		box.GetComponent<Rigidbody>().mass = length * height * width * density;
		box.GetComponent<Rigidbody>().maxAngularVelocity = 20f;
		PushObject pushObject = box.AddComponent<PushObject>();
		pushObject.fallDistance = 400f;
		SetParenting();
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			base.transform.localScale = remoteControl.transform.localScale;
			length = remoteControl.transform.localScale.z;
			width = remoteControl.transform.localScale.x;
			height = remoteControl.transform.localScale.y;
		}
		if (length < 0.0001f)
		{
			length = 0.0001f;
		}
		if (width < 0.0001f)
		{
			width = 0.0001f;
		}
		if (height < 0.0001f)
		{
			height = 0.0001f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, remoteControl.scale.y, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
	}

	private void SetParenting()
	{
		box.transform.parent = base.transform.parent;
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		}
	}

	public void SetupUVs()
	{
		if (objectType == PushObjectType.box)
		{
			Mesh mesh = box.GetComponent<MeshFilter>().mesh;
			Vector2[] uv = mesh.uv;
			float num = 1f;
			float num2 = 1f;
			uv[4].x = 0f;
			uv[4].y = 0f;
			uv[5].x = width * num;
			uv[5].y = height * num2;
			uv[6].x = width * num;
			uv[6].y = 0f;
			uv[7].x = 0f;
			uv[7].y = height * num2;
			uv[0].x = 0f;
			uv[0].y = 0f;
			uv[1].x = width * num;
			uv[1].y = height * num2;
			uv[2].x = 0f;
			uv[2].y = height * num2;
			uv[3].x = width * num;
			uv[3].y = 0f;
			uv[12].x = 0f;
			uv[12].y = 0f;
			uv[13].x = height * num;
			uv[13].y = length * num2;
			uv[14].x = height * num;
			uv[14].y = 0f;
			uv[15].x = 0f;
			uv[15].y = length * num2;
			uv[20].x = 0f;
			uv[20].y = 0f;
			uv[21].x = height * num;
			uv[21].y = length * num2;
			uv[22].x = height * num;
			uv[22].y = 0f;
			uv[23].x = 0f;
			uv[23].y = length * num2;
			uv[8].x = width * num;
			uv[8].y = 0f;
			uv[9].x = 0f;
			uv[9].y = length * num2;
			uv[10].x = 0f;
			uv[10].y = 0f;
			uv[11].x = width * num;
			uv[11].y = length * num2;
			uv[16].x = width * num;
			uv[16].y = 0f;
			uv[17].x = 0f;
			uv[17].y = length * num2;
			uv[18].x = 0f;
			uv[18].y = 0f;
			uv[19].x = width * num;
			uv[19].y = length * num2;
			mesh.uv = uv;
		}
	}
}
